<?php 

class CartController extends CommonController{



public function index(){
//	p($_POST);
	$cart = new Cart();
	if(IS_POST){
		$gid = $_POST['gid'];
		$Ginfo = K('Shop')->where("gid={$gid}")->find();
		
		$price = rtrim($_POST['price'],',');
		$price=explode(',', $price);
////		p($specids);
		foreach ($price as $v) {
			$spec[] = M('goods_attr')->where("gtid={$v}")->getField('gtvalue');
		}
//		p($spec);
		$spec = implode(',', $spec);
		$data = array(
			'id' => $_POST['gid'],
			'name' => $_POST['gname'],
			'num' => $_POST['num'],
			'price' => $_POST['specid'],
			'options'=>array(
		         "pic"=> $Ginfo['pic'],
		         "marketprice" =>$Ginfo['marketprice'],
		         'color'=>$spec
		       )
		);
		$cart->add($data);
//		p($_SESSION);
		

		$this->success('加入购物车成功',U('index'));
		
	}
	$data = $_SESSION['cart'];
//		p($data);
		$this->assign('data',$data);
	
	$this->display();
}

public function AddDel(){
//	p($_POST);
//p($_SESSION);
	    if(IS_AJAX){
			$sid = Q('post.sid');
			$num = Q('post.num',0,'intval');
			$data = array(
			'sid'=>$sid,// 唯一 sid，添加购物车时自动生成
			'num'=>$num
		);
	
		 Cart::update($data);
		 $info= Cart::getAllData();
//		  p($info);
		$m=$info['total'];
		$g=$info['total_rows'];
//		
		 $info=$info['goods'][$sid];
		 $info['totalall']=$m;
		$info['total_rows']=$g;

		echo json_encode($info);
		exit;
		}
	}




	/**
	 * 删除商品
	 */
	public function delGoods(){
	    $sid = Q('get.sid');
		Cart::del($sid);
		$this->success('删除成功');
	}

}

 ?>